#include "thread_pool.h"
#include <mysql/mysql.h>

extern MYSQL* conn;

void rmfileCommand(task_t* task){
    //获得filename
    char filename[128] = {0};
    strcpy(filename, task->data);
    printf("filename: %s\n", filename);

    //获得userid
    int userid = -1;
    getUserid(task->peerfd, &userid);
    printf("userid: %d\n", userid);

    //查询当前的pwdid
    int pwdid = -1;
    char query[512] = {0};
    sprintf(query,
            "SELECT pwdid FROM t_user WHERE userid=%d",
            userid);
    mysql_query(conn, query);
    MYSQL_RES* res = mysql_store_result(conn);
    MYSQL_ROW row = mysql_fetch_row(res);
    pwdid = atoi(row[0]);
    mysql_free_result(res);
    printf("pwdid: %d\n", pwdid);

    //查询这个filename对应的fid
    int fid = -1;
    memset(query, 0, sizeof(query));
    sprintf(query,
            "SELECT fid FROM t_file WHERE owner_id=%d AND parent_id=%d AND filename='%s' AND type='f'",
            userid, pwdid, filename);
    mysql_query(conn, query);
    res = mysql_store_result(conn);
    int count = mysql_num_rows(res);
    if(count == 0){     //文件不存在
        mysql_free_result(res);
        fprintf(stderr, "File id not exists.\n");
        sendMsg(task->peerfd, MSG_TYPE_NOT_EXIST, "File is not exists.");;
        return;
    } else {    //文件存在
        row = mysql_fetch_row(res);
        pwdid = atoi(row[0]);
        mysql_free_result(res);
        fid = atoi(row[0]);
        printf("fid: %d\n", fid);
    }

    //删除该文件
    memset(query, 0, sizeof(query));
    sprintf(query,
            "DELETE FROM t_file WHERE fid=%d",
            fid);
    mysql_query(conn, query);
    printf("Rmfile successful.\n");
    sendMsg(task->peerfd, MSG_TYPE_SUCCESS, "Rmfile successfully.");
    syslog(LOG_INFO, "User %d rmfile %d successfully.", userid, fid);

    
}


void rmfileCommandi1(task_t* task){
    // 删除文件
    int ret = unlink(task->data);
    train_t train = {0};
    switch(ret){
    case 0:
        strcpy(train.buff, "remove the file successfully.");
        train.len = strlen(train.buff);
        break;
    case ENOENT:
        strcpy(train.buff, "the file you give is not exist.");
        train.len = strlen(train.buff);
        break;
    case EACCES:
        strcpy(train.buff, "you don't have the right to remove the file.");
        train.len = strlen(train.buff);
        break;
    case EPERM:
        strcpy(train.buff, "the file can not be remove(maybe it's a directory).");
        train.len = strlen(train.buff);
        break;
    case EBUSY:
        strcpy(train.buff, "the file is used by other process.");
        train.len = strlen(train.buff);
        break;
    case EISDIR:
        strcpy(train.buff, "the aim is a directory.");
        train.len = strlen(train.buff);
        break;
    default:
        strcpy(train.buff, "remove the file failure.");
        train.len = strlen(train.buff);
        break;
    }
    // 将反馈信息传送回client
    sendn(task->peerfd, &train, sizeof(train.len) + train.len); 
    syslog(LOG_INFO, "User execute RMFILE command: %s.", train.buff);
}
